package event

import (
	"alex/ptp1588boundaryclock/datatypes"
)

type Delay_ReqMessage struct {
	Timestamp *datatypes.TimeStamp
}

func (a* Delay_ReqMessage) Init() {
	a.Timestamp = new(datatypes.TimeStamp)
}

// =====================================================================================================================
// Write Delay_Req Message
// =====================================================================================================================

// Writes the Delay_Req Message
func (a *Delay_ReqMessage) Write(delay_req []byte, done chan bool) {
	// 13.6.2.1 originTimestamp (Timestamp)
	// 11.3.2 The originTimestamp shall be set to 0 or an estimate no worse than ±1 s of the egress time of the Delay_Req message.
	for i := 0; i < 10; i++ {
		delay_req[i] = 0
	}
	done <- true
}

// =====================================================================================================================
// Read Delay_Req Message
// =====================================================================================================================

// Reads, checks and sets the Delay_Req Message values
func (a* Delay_ReqMessage) Read(delay_req []byte) (check bool) {
	check = true
	// 13.6.2.1 originTimestamp (Timestamp)
	a.readTimestamp(delay_req[:10])
	return
}

// Reads and sets the 13.6.2.1 originTimestamp (Timestamp)
func (a* Delay_ReqMessage) readTimestamp(timestamp []byte) {
// we use uint64 since uint48 is not available in Go
	a.Timestamp.SecondsField = uint64(timestamp[0]) << 40 | uint64(timestamp[1]) << 32 | uint64(timestamp[2]) <<
			24 | uint64(timestamp[3]) << 16 | uint64(timestamp[4]) << 8 | uint64(timestamp[5])
	a.Timestamp.NanosecondsField = uint32(timestamp[6]) << 24 | uint32(timestamp[7]) << 16 | uint32(timestamp[8]) <<
			8 | uint32(timestamp[9])
}
